<?php
// 通知消息
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Message;
use App\Models\Topic;

class ProcessMessage implements ShouldQueue
{

    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     * @param Message $message
     */
    public function handle(Message $message, Topic $topic)
    {

        $nowTime = date('Y-m-d H:i:s');
        $addData = [
            'user_id' => $this->data['to_user'] ? $this->data['to_user'] : $topic->where('id', $this->data['topic_id'])->value('user_id'),
            'from_user_id' => $this->data['user_id'],
            'content' => '回复了你：' . $this->data['content'],
            'relation_type' => 'topic',
            'relation_id' => $this->data['topic_id'],
            'created_at' => $nowTime,
            'updated_at' => $nowTime
        ];

        $message->insert($addData);
    }
}
